2020-2021 Sunseeker Telemetry and Lighting System
usci_b_i2c_ex1_masterRxSingle.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
51 //
52 //*****************************************************************************
53 //*****************************************************************************
54 //
55 //Set the address for slave module. This is a 7-bit address sent in the
56 //following format:
57 //[A6:A5:A4:A3:A2:A1:A0:RS]
58 //
59 //A zero in the "RS" position of the first byte means that the master
60 //transmits (sends) data to the selected slave, and a one in this position
61 //means that the master receives data from the slave.
62 //
63 //*****************************************************************************
64 #define SLAVE_ADDRESS 0x48
65 
66 uint8_t receivedData;
67 
68 void main (void)
69 {
70  //Stop WDT
71  WDT_A_hold(WDT_A_BASE);
72 
73  //Assign I2C pins to USCI_B0
74  GPIO_setAsPeripheralModuleFunctionInputPin(
75  GPIO_PORT_P3,
76  GPIO_PIN1 + GPIO_PIN2
77  );
78 
79  //Initialize Master
80  USCI_B_I2C_initMasterParam param = {0};
81  param.selectClockSource = USCI_B_I2C_CLOCKSOURCE_SMCLK;
82  param.i2cClk = UCS_getSMCLK();
83  param.dataRate = USCI_B_I2C_SET_DATA_RATE_400KBPS;
84  USCI_B_I2C_initMaster(USCI_B0_BASE, &param);
85 
86  //Specify slave address
87  USCI_B_I2C_setSlaveAddress(USCI_B0_BASE,
89  );
90 
91  //Set Master in receive mode
92  USCI_B_I2C_setMode(USCI_B0_BASE,
93  USCI_B_I2C_RECEIVE_MODE
94  );
95 
96  //Enable I2C Module to start operations
97  USCI_B_I2C_enable(USCI_B0_BASE);
98 
99  //Enable master Receive interrupt
100  USCI_B_I2C_clearInterrupt(USCI_B0_BASE,
101  USCI_B_I2C_RECEIVE_INTERRUPT
102  );
103  USCI_B_I2C_enableInterrupt(USCI_B0_BASE,
104  USCI_B_I2C_RECEIVE_INTERRUPT
105  );
106 
107  while (1)
108  {
109  //Initiate command to receive a single character from Slave
110  USCI_B_I2C_masterReceiveSingleStart(USCI_B0_BASE);
111 
112  //Enter low power mode 0 with interrupts enabled.
113  //Wait until character is received.
114  __bis_SR_register(LPM0_bits + GIE);
115  __no_operation();
116  }
117 }
118 
119 //******************************************************************************
120 //
121 //This is the USCI_B0 interrupt vector service routine.
122 //
123 //******************************************************************************
124 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
125 #pragma vector=USCI_B0_VECTOR
126 __interrupt
127 #elif defined(__GNUC__)
128 __attribute__((interrupt(USCI_B0_VECTOR)))
129 #endif
130 void USCI_B0_ISR (void)
131 {
132  switch (__even_in_range(UCB0IV,12)){
133  //Vector 10: Data received - RXIFG
134  case USCI_I2C_UCRXIFG:
135  {
136  //Grab data from data register
137  receivedData = USCI_B_I2C_masterReceiveSingle(
138  USCI_B0_BASE);
139 
140  //Exit low power mode 0 and disable GIE on interrupt exit
141  __bic_SR_register_on_exit(LPM0_bits + GIE);
142 
143  break;
144  }
145 
146  default: break;
147  }
148 }
149 
MPU_initThreeSegmentsParam param
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
uint8_t receivedData
void main(void)
void USCI_B0_ISR(void)
#define SLAVE_ADDRESS